package com.cro;

import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;


public class ScanActivity extends Activity implements SurfaceHolder.Callback {
	private SurfaceView sv;
	private SurfaceHolder sh;
	private Camera camera;
	    
	    @Override
		protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        setContentView(R.layout.main);
	        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	        DisplayMetrics dm = new DisplayMetrics();
	        getWindowManager().getDefaultDisplay().getMetrics(dm);
	        
	        SlidingDrawer sd = (SlidingDrawer)findViewById(R.id.slidingDrawer1);
	        sd.open();
	        Button bt = (Button)findViewById(R.id.b);
	        sv = (SurfaceView)findViewById(R.id.sv);
	        sh = sv.getHolder();
	        sh.addCallback(this);
	        sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	        sd.setOnDrawerCloseListener(new OnDrawerCloseListener() {
				
				@Override
				public void onDrawerClosed() {
					// TODO Auto-generated method stub
					surfaceCreated(sh);
					initCamera();
				}
			});
	        sd.setOnDrawerOpenListener(new OnDrawerOpenListener() {
				
				@Override
				public void onDrawerOpened() {
					// TODO Auto-generated method stub
					closeCamera();
				};
			});
	        bt.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
				}
			});
	    }
	    private void initCamera(){
	    	camera = Camera.open();
	    	Camera.Parameters parameters = camera.getParameters();
	    	parameters.setPictureFormat(PixelFormat.JPEG);
	    	parameters.setPreviewSize(320, 240);
	    	parameters.setPictureSize(320, 240);
	    	camera.setParameters(parameters);
	    	try {
				camera.setPreviewDisplay(sh);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.print("1212");
				e.printStackTrace();
			}
	    	camera.startPreview();
	    }
	    
	    private void closeCamera(){
	    	camera.release();
	    	camera = null;
	    }
		@Override
		public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void surfaceCreated(SurfaceHolder arg0) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void surfaceDestroyed(SurfaceHolder arg0) {
			// TODO Auto-generated method stub
			
		}
}


